var currentRuleId=0;
function submitRule(){
    var data={
        "rule_id":currentRuleId,
        "address_type":$("input[name='address_type']:checked").val(),
        "address_first":$('#address_first').val(),
        "address_second":$('#address_second').val(),
        "port_type":$("input[name='port_type']:checked").val(),
        "port_first":$('#port_first').val(),
        "port_second":$('#port_second').val(),
        "protocol_type":$("input[name='protocol_type']:checked").val(),
        "rate_value":$('#rate_value').val(),
    };
    $.post("?index/addRule",
        data,
        function(result){
            $("#addRuleModal").modal('hide');
           //alert(result["info"]);
           getRule();
        },
        "json"
    );

   // alert(data["address-type"]);
}
function getRule(){
    $.post("?index/getRule",
    {},
    function(result){
        $("#tableRule").empty();
        var rules = result["rules"];
        var html = '';
        rules.forEach(function(rule){
            html+="<tr>"+
            "<td>"+rule['rule_index']+"</td>"+
            "<td>"+rule['address_first']+"/"+rule['address_second']+"</td>";
            if(rule['port_type']==1){
                html+="<td>"+"range"+rule['port_first']+"/"+rule['port_second']+"</td>";
            }else{
                html+="<td>"+"tags"+rule['port_first']+"/"+rule['port_second']+"</td>";
            }
            
            html+="<td>"+rule['protocol_type']+"</td>"+
            "<td>"+rule['rate_value']+"</td>"+
            "<td>"+
                '<span class="badge badge-primary badge-pill tableButton" onclick="editRule('+rule['rule_index']+')">编辑</span>'+
                '<span class="badge badge-danger badge-pill tableButton" onclick="delRule('+rule['rule_index']+')">删除</span>'+
            "</td>"+
        "</tr>";
        });
        $("#tableRule").html(html);

       
    },
    "json"
    );
}
function delRule(rule_index){
    currentRuleId = rule_index;
    $('#delRuleModelContent').html("确定删除【"+rule_index+"】号规则吗？");
    $('#delRuleModel').modal("show");
}
function delRuleSure(){
    $.post("?index/delRule",
    {rule_id:currentRuleId},
    function(result){
       
        $('#delRuleModel').modal("hide");
        getRule();
    },
    "json"
    );
    
}
function editRule(rule_index){
    currentRuleId = rule_index;
    $.post("?index/getRule",
    {rule_id:currentRuleId},
    function(result){
        //$("input[name='address_type'][value='" + result['rule']['address_type'] + "']").prop("checked", true);
        $("input[name='address_type']").eq(result['rule']['address_type']-1).prop("checked", true);
        $('#address_first').val(result['rule']['address_first']);
        $('#address_second').val(result['rule']['address_second']);
        $("input[name='port_type']").eq(result['rule']['port_type']-1).prop("checked", true);
        $('#port_first').val(result['rule']['port_first']);
        $('#port_second').val(result['rule']['port_second']);
        $("input[name='protocol_type']").eq(result['rule']['protocol_type']-1).prop("checked", true);
        $('#rate_value').val(result['rule']['rate_value']);
        $("#addRuleModalTitle").html("修改【"+currentRuleId+"】号规则");
        $('#addRuleModal').modal("show");  
    },
    "json"
    );
}
(function ($) {
    "use strict"; // Start of use strict
    $('#sidebarCollapse').on('click', function () {
        $('#sidebar').toggleClass('active');
        $(this).toggleClass("ti-control-skip-backward ti-control-skip-forward");
    });
    /*Loader Javascript
    -------------------*/
    
    var window_var = $(window);
    window_var.on('load', function () {
        $(".loading").fadeOut("fast");
    });
    //弹出添加规则对话框
    $("#buttonAddRule").on('click', function () {
        currentRuleId = 0;//当前选中id为0
        $("#addRuleModalTitle").html("添加规则");
        $("#addRuleModal").modal('show');
        return false;
    });
    // scroll to top
    $(window).on('scroll',function () {
        if ($(this).scrollTop() > 50) {
            $('#back-to-top').fadeIn();
        } else {
            $('#back-to-top').fadeOut();
        }
    });
    // scroll body to 0px on click
    $('#back-to-top').on('click', function () {
        $('body,html').animate({
            scrollTop: 0
        }, 800);
        return false;
    });
    // scroll to top End

    // theme color change
    var theme_settings = $(".theme-settings").find(".theme-color");
    theme_settings.on('click', function () {
        var val = $(this).attr('data-color');
        $('#style_theme').attr('href', 'css/' + val + '.css');
        $(".logo-change").attr('src', 'img/logo-' + val + '.png');

        theme_settings.removeClass('theme-active');
        theme_settings.addClass('theme-active');
        return false;
    });
    $(".theme-click").on('click', function () {
        $("#switcher").toggleClass("active");
        return false;
    });
    // theme color change End

    // fullscreen function
    $(".fullscreen").on('click', function () {
        if (document.webkitCurrentFullScreenElement == null) {
            document.documentElement.webkitRequestFullScreen();
        } else {
            document.webkitCancelFullScreen();
        }
    });
    // fullscreen function End
    //刷新首页上的规则信息
    getRule();
})(jQuery);
